﻿using System.Windows.Controls;
using DeepEarth.Map.Core;
using System.Windows;
using System.ComponentModel;
using System.Windows.Data;
using DeepEarth.Core.Controls;

namespace DeepEarth.Toolkit.Common
{
    public class MapInteractionControl : ContentControl
    {
        public static readonly DependencyProperty MapProperty =
            DependencyProperty.Register("Map", typeof(IMap), typeof(MapInteractionControl), new PropertyMetadata(MapPropertyChanged));

        public IMap Map
        {
            get { return (IMap)GetValue(MapProperty); }
            set { SetValue(MapProperty, value); }
        }

        private static void MapPropertyChanged(DependencyObject sender, DependencyPropertyChangedEventArgs args)
        {
            var self = sender as MapInteractionControl;

            if (args.NewValue != null)
            {
                IMap map = args.NewValue as IMap;
                Binding viewBinding = new Binding(map.ViewDependencyPropertyPath) { Source = map, Mode = BindingMode.TwoWay };
                self.SetBinding(ViewProperty, viewBinding);
            }

            self.MapChanged((IMap)args.NewValue, (IMap)args.OldValue);
        }

        protected virtual void MapChanged(IMap newMap, IMap oldMap)
        {

        }

        public static readonly DependencyProperty ViewProperty =
            DependencyProperty.Register("View", typeof(MapView), typeof(MapInteractionControl), new PropertyMetadata(MapViewPropertyChanged));

        public MapView View
        {
            get { return (MapView)GetValue(ViewProperty); }
            set { SetValue(ViewProperty, value); }
        }

        private static void MapViewPropertyChanged(DependencyObject sender, DependencyPropertyChangedEventArgs args)
        {
            var self = sender as MapInteractionControl;
            self.ViewChanged(args.NewValue as MapView, args.OldValue as MapView);
        }

        protected virtual void ViewChanged(MapView newView, MapView oldView)
        {
            if (oldView != null)
            {
                oldView.PropertyChanged -= ViewPropertyChanged;
            }
            if (newView != null)
            {
                newView.PropertyChanged += ViewPropertyChanged;
            }               
        }

        protected virtual void ViewPropertyChanged(object sender, PropertyChangedEventArgs args)
        {

        }

        
        public static readonly DependencyProperty GeoItemsProperty =
            DependencyProperty.Register("GeoItems", typeof(GeoItems), typeof(MapInteractionControl), new PropertyMetadata(GeoItemsPropertyChanged));

        public GeoItems GeoItems
        {
            get { return (GeoItems)GetValue(GeoItemsProperty); }
            set { SetValue(GeoItemsProperty, value); }
        }

        private static void GeoItemsPropertyChanged(DependencyObject sender, DependencyPropertyChangedEventArgs args)
        {
            var self = sender as MapInteractionControl;

            self.GeoItemsChanged((GeoItems)args.NewValue, (GeoItems)args.OldValue);
        }

        protected virtual void GeoItemsChanged(GeoItems newMap, GeoItems oldMap)
        {

        }

		public bool IsVisible
		{
			get { return (bool)GetValue(IsVisibleProperty); }
			set { SetValue(IsVisibleProperty, value); }
		}

		// Using a DependencyProperty as the backing store for IsVisible.  This enables animation, styling, binding, etc...
		public static readonly DependencyProperty IsVisibleProperty =
			DependencyProperty.Register("IsVisible", typeof(bool), typeof(MapInteractionControl), new PropertyMetadata(IsVisiblePropertyChanged));

		private static void IsVisiblePropertyChanged(DependencyObject sender, DependencyPropertyChangedEventArgs args)
		{
			var self = (MapInteractionControl) sender;
			var newValue = (bool)args.NewValue;

			self.Visibility = newValue ? Visibility.Visible : Visibility.Collapsed;
		}
    }
}
